<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>网上书城会员登录</title>
    <!-- shortcut icon导入ico图标 -->
    <link rel="shortcut icon" href="image/favicon.ico" />
    <!-- stylesheet导入css文件 -->
    <link rel="stylesheet" href="css/index.css" />
    <!-- 导入vue框架 -->
    <script src="js/vue.js"></script>
    <!-- 导入饿了么框架 -->
    <script src="js/index.js"></script>
    <!-- 导入axios异步框架 -->
    <script src="js/axios.min.js"></script>
    <style>
      #myForm {
        width: 600px;
        margin: 100px auto;
        /*居中*/
        background-color: white;
        border-radius: 3px;
        padding: 20px;
      }

      #myDate {
        width: 600px;
        margin: auto;
      }

      body {
        background: url("image/bg.jpg");
      }
    </style>
  </head>

  <body>
    <div id="app">
      <div id="myForm">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input show-password v-model="ruleForm.password"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="login('ruleForm')"
              >登录</el-button
            >
            <el-button @click="register()">注册</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <script>
      // 使用vue框架，
      var app = new Vue({
        el: "#app", // 要托管的根节点
        methods: {
          register() {
            location = "register.html";
          },
          login(formName) {
            this.$refs[formName].validate((valid) => {
              if (valid) {
                axios
                  .post("http://127.0.0.1:8080/api/login", app.ruleForm)
                  .then(function (response) {
                    if (response.data) {
                      app.$message({
                        showClose: true,
                        message: "登录成功",
                        type: "success",
                        // 提示框关闭的时候会执行的函数
                        onClose: function () {
                          localStorage.setItem("token", response.data);
                          location = "index.html";
                        },
                      });
                    } else {
                      app.$message({
                        showClose: true,
                        message: "用户名或密码错误，登录失败",
                        type: "error",
                      });
                    }
                  });
                // 提交数据去后台注册保存
              } else {
                return false;
              }
            });
          },
        },
        mounted() {
          if (self != top) {
            top.location = self.location;
            return;
          }
          var letter = localStorage.getItem("letter");
          var msg = letter == 1 ? "请先登录" : "token已过期，请重新登录！";
          if (letter) {
            this.$message({
              message: msg,
              type: "warning",
            });
            localStorage.removeItem("letter");
          }
        },
        data() {
          return {
            ruleForm: {
              username: null,
              password: null,
            },
            rules: {
              username: [
                { required: true, message: "请输入用户名称", trigger: "blur" },
                {
                  min: 5,
                  max: 10,
                  message: "长度在 5 到 10 个字符",
                  trigger: "blur",
                },
              ],
              password: [
                { required: true, message: "请输入密码", trigger: "blur" },
                {
                  min: 5,
                  max: 10,
                  message: "长度在 5 到 10 个字符",
                  trigger: "blur",
                },
              ],
            },
          };
        }, // 数据区域，用于存储页面交互的数据变量
      });
    </script>
  </body>
</html>
